home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / string.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  19KB  |  573 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. """A collection of string operations (most are no longer used).
  5.  
  6. Warning: most of the code you see here isn't normally used nowadays.
  7. Beginning with Python 1.6, many of these functions are implemented as
  8. methods on the standard string object. They used to be implemented by
  9. a built-in module called strop, but strop is now obsolete itself.
  10.  
  11. Public module variables:
  12.  
  13. whitespace -- a string containing all characters considered whitespace
  14. lowercase -- a string containing all characters considered lowercase letters
  15. uppercase -- a string containing all characters considered uppercase letters
  16. letters -- a string containing all characters considered letters
  17. digits -- a string containing all characters considered decimal digits
  18. hexdigits -- a string containing all characters considered hexadecimal digits
  19. octdigits -- a string containing all characters considered octal digits
  20. punctuation -- a string containing all characters considered punctuation
  21. printable -- a string containing all characters considered printable
  22.  
  23. """
  24. whitespace = ' \t\n\r\x0b\x0c'
  25. lowercase = 'abcdefghijklmnopqrstuvwxyz'
  26. uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  27. letters = lowercase + uppercase
  28. ascii_lowercase = lowercase
  29. ascii_uppercase = uppercase
  30. ascii_letters = ascii_lowercase + ascii_uppercase
  31. digits = '0123456789'
  32. hexdigits = digits + 'abcdef' + 'ABCDEF'
  33. octdigits = '01234567'
  34. punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  35. printable = digits + letters + punctuation + whitespace
  36. l = map(chr, xrange(256))
  37. _idmap = str('').join(l)
  38. del l
  39.  
  40. def capwords(s, sep = None):
  41.     '''capwords(s [,sep]) -> string
  42.  
  43.     Split the argument into words using split, capitalize each
  44.     word using capitalize, and join the capitalized words using
  45.     join.  If the optional second argument sep is absent or None,
  46.     runs of whitespace characters are replaced by a single space
  47.     and leading and trailing whitespace are removed, otherwise
  48.     sep is used to split and join the words.
  49.  
  50.     '''
  51.     if not sep:
  52.         pass
  53.     return ' '.join((lambda .0: pass)(s.split(sep)))
  54.  
  55. _idmapL = None
  56.  
  57. def maketrans(fromstr, tostr):
  58.     '''maketrans(frm, to) -> string
  59.  
  60.     Return a translation table (a string of 256 bytes long)
  61.     suitable for use in string.translate.  The strings frm and to
  62.     must be of the same length.
  63.  
  64.     '''
  65.     global _idmapL
  66.     if len(fromstr) != len(tostr):
  67.         raise ValueError, 'maketrans arguments must have same length'
  68.     if not _idmapL:
  69.         _idmapL = list(_idmap)
  70.     L = _idmapL[:]
  71.     fromstr = map(ord, fromstr)
  72.     for i in range(len(fromstr)):
  73.         L[fromstr[i]] = tostr[i]
  74.     
  75.     return ''.join(L)
  76.  
  77. import re as _re
  78.  
  79. class _multimap:
  80.     '''Helper class for combining multiple mappings.
  81.  
  82.     Used by .{safe_,}substitute() to combine the mapping and keyword
  83.     arguments.
  84.     '''
  85.     
  86.     def __init__(self, primary, secondary):
  87.         self._primary = primary
  88.         self._secondary = secondary
  89.  
  90.     
  91.     def __getitem__(self, key):
  92.         
  93.         try:
  94.             return self._primary[key]
  95.         except KeyError:
  96.             return self._secondary[key]
  97.  
  98.  
  99.  
  100.  
  101. class _TemplateMetaclass(type):
  102.     pattern = '\n    %(delim)s(?:\n      (?P<escaped>%(delim)s) |   # Escape sequence of two delimiters\n      (?P<named>%(id)s)      |   # delimiter and a Python identifier\n      {(?P<braced>%(id)s)}   |   # delimiter and a braced identifier\n      (?P<invalid>)              # Other ill-formed delimiter exprs\n    )\n    '
  103.     
  104.     def __init__(cls, name, bases, dct):
  105.         super(_TemplateMetaclass, cls).__init__(name, bases, dct)
  106.         if 'pattern' in dct:
  107.             pattern = cls.pattern
  108.         else:
  109.             pattern = _TemplateMetaclass.pattern % {
  110.                 'delim': _re.escape(cls.delimiter),
  111.                 'id': cls.idpattern }
  112.         cls.pattern = _re.compile(pattern, _re.IGNORECASE | _re.VERBOSE)
  113.  
  114.  
  115.  
  116. class Template:
  117.     '''A string class for supporting $-substitutions.'''
  118.     __metaclass__ = _TemplateMetaclass
  119.     delimiter = '$'
  120.     idpattern = '[_a-z][_a-z0-9]*'
  121.     
  122.     def __init__(self, template):
  123.         self.template = template
  124.  
  125.     
  126.     def _invalid(self, mo):
  127.         i = mo.start('invalid')
  128.         lines = self.template[:i].splitlines(True)
  129.         if not lines:
  130.             colno = 1
  131.             lineno = 1
  132.         else:
  133.             colno = i - len(''.join(lines[:-1]))
  134.             lineno = len(lines)
  135.         raise ValueError('Invalid placeholder in string: line %d, col %d' % (lineno, colno))
  136.  
  137.     
  138.     def substitute(self, *args, **kws):
  139.         if len(args) > 1:
  140.             raise TypeError('Too many positional arguments')
  141.         if not args:
  142.             mapping = kws
  143.         elif kws:
  144.             mapping = _multimap(kws, args[0])
  145.         else:
  146.             mapping = args[0]
  147.         
  148.         def convert(mo):
  149.             if not mo.group('named'):
  150.                 pass
  151.             named = mo.group('braced')
  152.             if named is not None:
  153.                 val = mapping[named]
  154.                 return '%s' % (val,)
  155.             if None.group('escaped') is not None:
  156.                 return self.delimiter
  157.             if None.group('invalid') is not None:
  158.                 self._invalid(mo)
  159.             raise ValueError('Unrecognized named group in pattern', self.pattern)
  160.  
  161.         return self.pattern.sub(convert, self.template)
  162.  
  163.     
  164.     def safe_substitute(self, *args, **kws):
  165.         if len(args) > 1:
  166.             raise TypeError('Too many positional arguments')
  167.         if not args:
  168.             mapping = kws
  169.         elif kws:
  170.             mapping = _multimap(kws, args[0])
  171.         else:
  172.             mapping = args[0]
  173.         
  174.         def convert(mo):
  175.             named = mo.group('named')
  176.             if named is not None:
  177.                 
  178.                 try:
  179.                     return '%s' % (mapping[named],)
  180.                 except KeyError:
  181.                     return self.delimiter + named
  182.                 
  183.  
  184.             braced = mo.group('braced')
  185.             if braced is not None:
  186.                 
  187.                 try:
  188.                     return '%s' % (mapping[braced],)
  189.                 except KeyError:
  190.                     return self.delimiter + '{' + braced + '}'
  191.                 
  192.  
  193.             if mo.group('escaped') is not None:
  194.                 return self.delimiter
  195.             if None.group('invalid') is not None:
  196.                 return self.delimiter
  197.             raise None('Unrecognized named group in pattern', self.pattern)
  198.  
  199.         return self.pattern.sub(convert, self.template)
  200.  
  201.  
  202. index_error = ValueError
  203. atoi_error = ValueError
  204. atof_error = ValueError
  205. atol_error = ValueError
  206.  
  207. def lower(s):
  208.     '''lower(s) -> string
  209.  
  210.     Return a copy of the string s converted to lowercase.
  211.  
  212.     '''
  213.     return s.lower()
  214.  
  215.  
  216. def upper(s):
  217.     '''upper(s) -> string
  218.  
  219.     Return a copy of the string s converted to uppercase.
  220.  
  221.     '''
  222.     return s.upper()
  223.  
  224.  
  225. def swapcase(s):
  226.     '''swapcase(s) -> string
  227.  
  228.     Return a copy of the string s with upper case characters
  229.     converted to lowercase and vice versa.
  230.  
  231.     '''
  232.     return s.swapcase()
  233.  
  234.  
  235. def strip(s, chars = None):
  236.     '''strip(s [,chars]) -> string
  237.  
  238.     Return a copy of the string s with leading and trailing
  239.     whitespace removed.
  240.     If chars is given and not None, remove characters in chars instead.
  241.     If chars is unicode, S will be converted to unicode before stripping.
  242.  
  243.     '''
  244.     return s.strip(chars)
  245.  
  246.  
  247. def lstrip(s, chars = None):
  248.     '''lstrip(s [,chars]) -> string
  249.  
  250.     Return a copy of the string s with leading whitespace removed.
  251.     If chars is given and not None, remove characters in chars instead.
  252.  
  253.     '''
  254.     return s.lstrip(chars)
  255.  
  256.  
  257. def rstrip(s, chars = None):
  258.     '''rstrip(s [,chars]) -> string
  259.  
  260.     Return a copy of the string s with trailing whitespace removed.
  261.     If chars is given and not None, remove characters in chars instead.
  262.  
  263.     '''
  264.     return s.rstrip(chars)
  265.  
  266.  
  267. def split(s, sep = None, maxsplit = -1):
  268.     '''split(s [,sep [,maxsplit]]) -> list of strings
  269.  
  270.     Return a list of the words in the string s, using sep as the
  271.     delimiter string.  If maxsplit is given, splits at no more than
  272.     maxsplit places (resulting in at most maxsplit+1 words).  If sep
  273.     is not specified or is None, any whitespace string is a separator.
  274.  
  275.     (split and splitfields are synonymous)
  276.  
  277.     '''
  278.     return s.split(sep, maxsplit)
  279.  
  280. splitfields = split
  281.  
  282. def rsplit(s, sep = None, maxsplit = -1):
  283.     '''rsplit(s [,sep [,maxsplit]]) -> list of strings
  284.  
  285.     Return a list of the words in the string s, using sep as the
  286.     delimiter string, starting at the end of the string and working
  287.     to the front.  If maxsplit is given, at most maxsplit splits are
  288.     done. If sep is not specified or is None, any whitespace string
  289.     is a separator.
  290.     '''
  291.     return s.rsplit(sep, maxsplit)
  292.  
  293.  
  294. def join(words, sep = ' '):
  295.     '''join(list [,sep]) -> string
  296.  
  297.     Return a string composed of the words in list, with
  298.     intervening occurrences of sep.  The default separator is a
  299.     single space.
  300.  
  301.     (joinfields and join are synonymous)
  302.  
  303.     '''
  304.     return sep.join(words)
  305.  
  306. joinfields = join
  307.  
  308. def index(s, *args):
  309.     '''index(s, sub [,start [,end]]) -> int
  310.  
  311.     Like find but raises ValueError when the substring is not found.
  312.  
  313.     '''
  314.     return s.index(*args)
  315.  
  316.  
  317. def rindex(s, *args):
  318.     '''rindex(s, sub [,start [,end]]) -> int
  319.  
  320.     Like rfind but raises ValueError when the substring is not found.
  321.  
  322.     '''
  323.     return s.rindex(*args)
  324.  
  325.  
  326. def count(s, *args):
  327.     '''count(s, sub[, start[,end]]) -> int
  328.  
  329.     Return the number of occurrences of substring sub in string
  330.     s[start:end].  Optional arguments start and end are
  331.     interpreted as in slice notation.
  332.  
  333.     '''
  334.     return s.count(*args)
  335.  
  336.  
  337. def find(s, *args):
  338.     '''find(s, sub [,start [,end]]) -> in
  339.  
  340.     Return the lowest index in s where substring sub is found,
  341.     such that sub is contained within s[start,end].  Optional
  342.     arguments start and end are interpreted as in slice notation.
  343.  
  344.     Return -1 on failure.
  345.  
  346.     '''
  347.     return s.find(*args)
  348.  
  349.  
  350. def rfind(s, *args):
  351.     '''rfind(s, sub [,start [,end]]) -> int
  352.  
  353.     Return the highest index in s where substring sub is found,
  354.     such that sub is contained within s[start,end].  Optional
  355.     arguments start and end are interpreted as in slice notation.
  356.  
  357.     Return -1 on failure.
  358.  
  359.     '''
  360.     return s.rfind(*args)
  361.  
  362. _float = float
  363. _int = int
  364. _long = long
  365.  
  366. def atof(s):
  367.     '''atof(s) -> float
  368.  
  369.     Return the floating point number represented by the string s.
  370.  
  371.     '''
  372.     return _float(s)
  373.  
  374.  
  375. def atoi(s, base = 10):
  376.     '''atoi(s [,base]) -> int
  377.  
  378.     Return the integer represented by the string s in the given
  379.     base, which defaults to 10.  The string s must consist of one
  380.     or more digits, possibly preceded by a sign.  If base is 0, it
  381.     is chosen from the leading characters of s, 0 for octal, 0x or
  382.     0X for hexadecimal.  If base is 16, a preceding 0x or 0X is
  383.     accepted.
  384.  
  385.     '''
  386.     return _int(s, base)
  387.  
  388.  
  389. def atol(s, base = 10):
  390.     '''atol(s [,base]) -> long
  391.  
  392.     Return the long integer represented by the string s in the
  393.     given base, which defaults to 10.  The string s must consist
  394.     of one or more digits, possibly preceded by a sign.  If base
  395.     is 0, it is chosen from the leading characters of s, 0 for
  396.     octal, 0x or 0X for hexadecimal.  If base is 16, a preceding
  397.     0x or 0X is accepted.  A trailing L or l is not accepted,
  398.     unless base is 0.
  399.  
  400.     '''
  401.     return _long(s, base)
  402.  
  403.  
  404. def ljust(s, width, *args):
  405.     '''ljust(s, width[, fillchar]) -> string
  406.  
  407.     Return a left-justified version of s, in a field of the
  408.     specified width, padded with spaces as needed.  The string is
  409.     never truncated.  If specified the fillchar is used instead of spaces.
  410.  
  411.     '''
  412.     return s.ljust(width, *args)
  413.  
  414.  
  415. def rjust(s, width, *args):
  416.     '''rjust(s, width[, fillchar]) -> string
  417.  
  418.     Return a right-justified version of s, in a field of the
  419.     specified width, padded with spaces as needed.  The string is
  420.     never truncated.  If specified the fillchar is used instead of spaces.
  421.  
  422.     '''
  423.     return s.rjust(width, *args)
  424.  
  425.  
  426. def center(s, width, *args):
  427.     '''center(s, width[, fillchar]) -> string
  428.  
  429.     Return a center version of s, in a field of the specified
  430.     width. padded with spaces as needed.  The string is never
  431.     truncated.  If specified the fillchar is used instead of spaces.
  432.  
  433.     '''
  434.     return s.center(width, *args)
  435.  
  436.  
  437. def zfill(x, width):
  438.     '''zfill(x, width) -> string
  439.  
  440.     Pad a numeric string x with zeros on the left, to fill a field
  441.     of the specified width.  The string x is never truncated.
  442.  
  443.     '''
  444.     if not isinstance(x, basestring):
  445.         x = repr(x)
  446.     return x.zfill(width)
  447.  
  448.  
  449. def expandtabs(s, tabsize = 8):
  450.     '''expandtabs(s [,tabsize]) -> string
  451.  
  452.     Return a copy of the string s with all tab characters replaced
  453.     by the appropriate number of spaces, depending on the current
  454.     column, and the tabsize (default 8).
  455.  
  456.     '''
  457.     return s.expandtabs(tabsize)
  458.  
  459.  
  460. def translate(s, table, deletions = ''):
  461.     '''translate(s,table [,deletions]) -> string
  462.  
  463.     Return a copy of the string s, where all characters occurring
  464.     in the optional argument deletions are removed, and the
  465.     remaining characters have been mapped through the given
  466.     translation table, which must be a string of length 256.  The
  467.     deletions argument is not allowed for Unicode strings.
  468.  
  469.     '''
  470.     if deletions or table is None:
  471.         return s.translate(table, deletions)
  472.     return None.translate(table + s[:0])
  473.  
  474.  
  475. def capitalize(s):
  476.     '''capitalize(s) -> string
  477.  
  478.     Return a copy of the string s with only its first character
  479.     capitalized.
  480.  
  481.     '''
  482.     return s.capitalize()
  483.  
  484.  
  485. def replace(s, old, new, maxreplace = -1):
  486.     '''replace (str, old, new[, maxreplace]) -> string
  487.  
  488.     Return a copy of string str with all occurrences of substring
  489.     old replaced by new. If the optional argument maxreplace is
  490.     given, only the first maxreplace occurrences are replaced.
  491.  
  492.     '''
  493.     return s.replace(old, new, maxreplace)
  494.  
  495.  
  496. try:
  497.     from strop import maketrans, lowercase, uppercase, whitespace
  498.     letters = lowercase + uppercase
  499. except ImportError:
  500.     pass
  501.  
  502.  
  503. class Formatter(object):
  504.     
  505.     def format(self, format_string, *args, **kwargs):
  506.         return self.vformat(format_string, args, kwargs)
  507.  
  508.     
  509.     def vformat(self, format_string, args, kwargs):
  510.         used_args = set()
  511.         result = self._vformat(format_string, args, kwargs, used_args, 2)
  512.         self.check_unused_args(used_args, args, kwargs)
  513.         return result
  514.  
  515.     
  516.     def _vformat(self, format_string, args, kwargs, used_args, recursion_depth):
  517.         if recursion_depth < 0:
  518.             raise ValueError('Max string recursion exceeded')
  519.         result = []
  520.         for literal_text, field_name, format_spec, conversion in self.parse(format_string):
  521.             if literal_text:
  522.                 result.append(literal_text)
  523.             if field_name is not None:
  524.                 (obj, arg_used) = self.get_field(field_name, args, kwargs)
  525.                 used_args.add(arg_used)
  526.                 obj = self.convert_field(obj, conversion)
  527.                 format_spec = self._vformat(format_spec, args, kwargs, used_args, recursion_depth - 1)
  528.                 result.append(self.format_field(obj, format_spec))
  529.                 continue
  530.         return ''.join(result)
  531.  
  532.     
  533.     def get_value(self, key, args, kwargs):
  534.         if isinstance(key, (int, long)):
  535.             return args[key]
  536.         return None[key]
  537.  
  538.     
  539.     def check_unused_args(self, used_args, args, kwargs):
  540.         pass
  541.  
  542.     
  543.     def format_field(self, value, format_spec):
  544.         return format(value, format_spec)
  545.  
  546.     
  547.     def convert_field(self, value, conversion):
  548.         if conversion is None:
  549.             return value
  550.         if None == 's':
  551.             return str(value)
  552.         if None == 'r':
  553.             return repr(value)
  554.         raise None('Unknown conversion specifier {0!s}'.format(conversion))
  555.  
  556.     
  557.     def parse(self, format_string):
  558.         return format_string._formatter_parser()
  559.  
  560.     
  561.     def get_field(self, field_name, args, kwargs):
  562.         (first, rest) = field_name._formatter_field_name_split()
  563.         obj = self.get_value(first, args, kwargs)
  564.         for is_attr, i in rest:
  565.             if is_attr:
  566.                 obj = getattr(obj, i)
  567.                 continue
  568.             obj = obj[i]
  569.         
  570.         return (obj, first)
  571.  
  572.  
  573.